package com.google.way1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class Way1Activity extends Activity {
	
	private AdapterForLinearLayout adapter;
	
	private MyLinearLayoutForListAdapter mLinearLayout;
	private List<Map<String, String>> list;

	@Override
	public void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.way1);
		
		init();
		setAdapterAndOnClickListener();
	}
	
	private void init(){
		mLinearLayout = (MyLinearLayoutForListAdapter)this.findViewById(R.id.mylinear_way1);
	}
	
	private void setAdapterAndOnClickListener(){
		list = new ArrayList<Map<String, String>>();
		for(int i=0;i<20;i++){
			Map<String, String> map = new HashMap<String, String>();
			map.put("key1", "name"+i);
			map.put("key2", "value"+i);
			list.add(map);
		}
		initAdapter(list);
		addLinearListener();
		
		mLinearLayout.setAdapter(adapter);

	}
	
	private void initAdapter(List<Map<String, String>> list){
		adapter = new AdapterForLinearLayout(this, R.layout.list_item, list, 
				new String[]{"key1", "key2"}, new int[]{R.id.tv_list_item_1, R.id.tv_list_item_2});
	}
	
	private void addLinearListener(){
				mLinearLayout.setOnClickListener(new OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                showToast(((TextView)v.findViewById(R.id.tv_list_item_1)).getText().toString());
//	            	showToast("-_-");
	            }
	        });
	}
	
	
	private void showToast(String text){
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
	
	
	
}
